<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            选箱退货
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="consignment" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">送检单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="inspection" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlcode" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">规格</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="drawing" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotcode" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱码</div>
                    <div class="aui-list-item-inner">
                        <input type="text" class="aui-input" id="boxnum" placeholder="请 输入/扫描箱码 查询" value="" />
                        <i class="aui-input-addon aui-iconfont aui-icon-scan " style="padding-right:10px;font-size: 1.6rem"
                            onclick="doScan();">
                        </i>
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:10%;" onclick="doSearch()">查询</div>
        <div class="aui-btn aui-btn-info" style="width:35%;margin-left:20%;" onclick="replaceBox()">拆分后箱码替换</div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td algin='center'>选择</td>
                        <td algin='center'>序号</td>
                        <td algin='center'>箱号</td>
                        <td algin='center'>数量</td>
                        <td algin='center'>状态</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
        <div>
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-input" style="width:2%">
                            <input type="checkbox" name = "quanxuan" id="quanxuan"  value="" style='color:red' onclick="allcheck()" />
                        </div>
                        <div class="aui-list-item-label" style="width:100%">全选</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                        <div class="aui-list-item-label" style="width:700px">当前已选择：</div>
                        <div class="aui-list-item-input" >
                            <input type="text" id="dqyxzNum" value="0" disabled="true" style='color:red' />
                        </div>
                        
                        <div class="aui-list-item-label" style="width:500px">总箱数：</div>
                        <div class="aui-list-item-input" style="width: 500px;">
                            <input type="text" id="num" value="0" disabled="true" style='color:red;'  />
                        </div>
                        
                    </div>
                </li>
            </ul>
        </div>
        
        
        <ul class="ui-selectee" id="tbodyHtml2">

        </ul>
    </div>
    <div style="height:60px;"></div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-btn aui-btn-info aui-btn-block"   onclick="returnGoods()">
            <i class="aui-iconfont  aui-text-epblue" style="font-size: 0.9rem">选中退货</i>
        </div>
    </footer>

    <!-- <div>
        <div style="height:60px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div> -->

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var consignment = ""; //发货单号
    var inspection = ""; //送检单号
    var mrlcode = ""; //物料号
    var drawing = ""; //规格
    var lotcode = ""; //批次
    var boxnum = ""; // 箱码
    var arrSave = [];
    var num = 0;
    var dqyxzCount = 0;

    var scanRule = ""; //条码解析规则
    var arrList = [];

    //初始化
    apiready = function () {

        console.log('route:'+JSON.stringify(api.pageParam));

        idSelector='boxnum'

        consignment = api.pageParam.consignment;//发货单号
        $("#consignment").val(consignment);
        inspection = api.pageParam.inspection;//送检单号
        $("#inspection").val(inspection);
        mrlcode = api.pageParam.mrlcode;//物料号
        $("#mrlcode").val(mrlcode);
        drawing = api.pageParam.drawing;//规格
        $("#drawing").val(drawing);
        lotcode = api.pageParam.lotcode;//批次
        $("#lotcode").val(lotcode);
        
        doSearch();
        
        //查询条码解析规则
        initScanRule();


    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

//查询扫描规则
function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            if (idSelector == 'boxnum') {
                $("#boxnum").val(ret);
                //箱码解析
                tmSplit(ret);
                doSearch();
            }
        });
    }
//扫描之后条码解析
function tmSplit(sm) {
         $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        var mrlCode = "";
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            mrlCode = arr[1];
        }
        var qty = "";
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            qty = arr[2];
        }
        var lotCode = "";
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            lotCode = arr[3];
        }
        var tmID = "";
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmID").val("");
        } else {
            tmID = arr[4];
        }
        var boxnum = "";
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            boxnum = arr[9];
        }

    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    $("#boxnum").val("");
                    //物料号
                    var mrlCode = map["mrlCode"];
                    //批次号
                    var lotCode = map["lotCode"];
                    //转移数量数量
                    var qty = map["qty"];
                    //条码
                    var tm = map["tm"];
                    //箱号
                    var boxnum = map["boxnum"];
                    //tmID
                    var tmID = map["tmID"];

                    $("#boxnum").val(boxnum);

                } else if (myobj.code == '1') {
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm);
                $("#tm").val(tm);
                $("#tm").focus();
                $("#tm").select();
                doSearch();
            }

        }
    });

    //查询
    function doSearch() {
        var arr = $("#boxnum").val().split(",");
        if (arr.length == 10) {
            var boxnum = arr[9];
        }else{
            var boxnum = $("#boxnum").val();
        }
        var un = localStorage.getItem('un');
        
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchInspectionBoxInfo";
        var data = {
            values: {
                consignmentQ: consignment,
                inspectionQ: inspection,
                boxnumQ: boxnum,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                $("#num").val(myobj.list.length)

                $("#dqyxzNum").val(0);
                $("input[name^='quanxuan']")[0].checked = false;

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
                        html += "<td ><input name='sub' type='checkbox' id='allboxs' onclick='checkClick()' /></td>";
                        html += "<td name='seqno'>" + (i + 1) + "</td>";
                        html += "<td name='boxnum'>" + list[i].boxnum + "</td>";
                        html += "<td name='qty'>" + list[i].qty + "</td>";

                        //验证状态
                        if (list[i].state == '已退货') {
                            html += "<td name='state' style='width:80px;background-color:red;'>" + list[i].state + "</td>";
                        }else{
                            html += "<td name='state'>" + list[i].state + "</td>";
                        } 
                        
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                    //失败
                } else if (myobj.code == "1") {

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    // 
    function allcheck() {
        dqyxzCount = 0;
        var nn = $("#quanxuan").is(":checked"); //判断th中的checkbox是否被选中，如果被选中则nn为true，反之为false
        if (nn == true) {
            var namebox = $("input[name^='sub']");  //获取name值为boxs的所有input
            for (i = 0; i < namebox.length; i++) {
                namebox[i].checked = true;    //js操作选中checkbox
            }
        }
        if (nn == false) {
            var namebox = $("input[name^='sub']");
            for (i = 0; i < namebox.length; i++) {
                namebox[i].checked = false;
            } 
        }

        //选中数据 
        // var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var check = $("input[id='allboxs']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        check.each(function () {
            dqyxzCount++;
        });

        $("#dqyxzNum").val(dqyxzCount);

    }

    // 表格选中单机事件
    function checkClick() {
        dqyxzCount = 0;
        //选中数据
        var check = $("input[id='allboxs']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        check.each(function () {
            dqyxzCount++;
        });

        $("#dqyxzNum").val(dqyxzCount);
    }

    // 拆分后箱码替换
    function replaceBox() {
        //发货单号
        var consignment = $("#consignment").val();

        //送检单号
        var inspection = $("#inspection").val();

        //送检单号非空校验
        if (inspection == "" || inspection == null || inspection == undefined) {
            api.toast({
                msg: "送检单号不能为空",
                duration: 3000
            });
            return;
        }

        api.openWin({
            name: 'uwmInspectionRejectBoxRepllace',
            url: 'uwmInspectionRejectBoxRepllace.html',
            pageParam: {
                inspection: inspection,
                consignment: consignment,
            }
        });
    }

    //退货
    function returnGoods() {
        //发货单号
        var consignment = $("#consignment").val();
        //送检单号
        var inspection = $("#inspection").val();
        //选中数据
        var check = $("input[id='allboxs']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        var flag = "";
        check.each(function () { //遍历

            var row = $(this).parent("td").parent("tr");
            var gyStatus = row.find("[name='state']").html();
            var boxnum = row.find("[name='boxnum']").html();
            count++;
            if (gyStatus == '已退货') {
                alert('箱【' + boxnum + '】已经退货！');
                flag = "E"
                return;
            }

        });
        if (flag == "E") {
            return;
        }
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }


        var list = [];

        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            var boxnum = row.find("[name='boxnum']").html();
            var mrlCode = row.find("[name='mrlCode']").html();
            var lotCode = row.find("[name='lotCode']").html();
            var qty = row.find("[name='qty']").html();

            list.push({
                "boxnum": boxnum,
                "mrlCode": mrlCode,
                "lotCode": lotCode,
                "qty": qty,
            });
        });
        //打开页面
        api.openWin({
            name: 'uwmInspectionRejectSelect',
            url: 'uwmInspectionRejectSelect' + '.html',
            pageParam: {
                consignment: consignment,
                inspection: inspection,
                list: list,
            },
        });

    }

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }


</script>

</html>